﻿<!--#include file="../lib/base.asp"-->
<!--#include file="../lib/upload_class.asp"-->
<%

	sub getjson(byval t0,byval t1,byval t2,byval t3)
		if t0=0 Then
			Cls.echo "{""url"":"""",""title"":"""",""state"":"""&t3&"""}"
		else
			Cls.echo "{""url"":"""&t1&""",""title"":"""&t2&""",""state"":"""&t3&"""}"
		end if
	end sub
	
	function get_uploaderr(byval t0)
		dim t1:t1=""
		select case t0
			case -1:t1="没有文件上传"
			case 1:t1="文件过大，上传失败"
			case 2:t1="不允许被上传的文件"
			case 3:t1="文件过大，且不允许被上传"
			case 4:t1="文件保存失败"
			case 5:t1="禁止上传此类文件"
		end select
		get_uploaderr=t1
	end function
	
	sub errdb()
		Cls.echo "错误的参数"
	end sub
	
	sub adddb(byval t0,byval t1,byval t2,byval t3,byval t4,byval t5,byval t6,byval t7,byval t8,byval t9)
		Cls.db.insert "attachment",array(array("classid",t9,0,0),array("cid",t8,0,0),array("filepath",t0,255,1),array("filename",t1,255,1),array("filesize",Cls.getnum(t2),255,1),array("oldname",t3,255,1),array("fileext",t4,50,1),array("ispic",Cls.is_pic("."&t4),1,0),array("createip",Cls.getip,255,1),array("createdate",t5,255,0),array("adminid",t6,0,0),array("userid",t7,0,0),array("fullname",t0&t1,0,1))
		if Cls.is_pic(t1)=1 then
			dim draw
			set draw=new Cls_draw
				draw.add t0&t1
			set draw=nothing
		end if
	end sub
	
	function getfile_ext(t0)
		dim t1
		select case lcase(t0)
			case "jpg","gif","png":t1="pic"
			case "docx":t1="doc"
			case "xlsx":t1="xls"
			case "pptx":t1="ppt"
			case "rar","zip","7z":t1="rar"
			case  "ppt","xls","doc","pdf","txt","flv","swf":t1=t0
			case else:t1="blank"
		end select
		getfile_ext=t1
	end function
	
	sub editordb()
		dim upload,filetype,uptype,classid,cid,pictitle
		filetype=Cls.fget("filetype",0)
		classid=Cls.getint(Cls.fget("classid",0),0)
		cid=Cls.getint(Cls.fget("cid",0),0)
		select case filetype
			case "2":uptype=Cls.getsys("uptype_pic")&"|"&Cls.getsys("uptype_video")&"|"&Cls.getsys("uptype_file")
			case else:uptype=Cls.getsys("uptype_pic")
		end select
		
		set upload=new Cls_upload
			upload.filetype=uptype
			upload.savepath=Cls.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=Cls.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			dim admin_id:admin_id=Cls.getint(Cls.fget("admin_id",0),0)
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				pictitle=upload.form("pictitle")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if pictitle="" then pictitle=oldname
				if Cls.strlen(errstr)>0 then
					getjson 0,"","",errstr
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,admin_id,0,cid,classid
					getjson 1,filename,pictitle,"SUCCESS"
				end if
			next
		set upload=nothing
	end sub
	
	sub swfdb()
		dim upload,filetype,uptype,classid,cid
		filetype=Cls.fget("filetype",0)
		select case filetype
			case "2":uptype="zip|rar|7z|doc|docx|ppt|pptx|xls|xls|pdf"
			case "3":uptype="swf|flv"
			case "4":uptype="gif|jpg|png|swf|flv|zip|rar|7z|doc|docx|ppt|pptx|xls|xls|pdf"
			case "5":uptype="swf"
			case else:uptype="gif|jpg|png"
		end select
		set upload=new Cls_upload
			upload.filetype=uptype
			upload.savepath=Cls.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=204800
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if Cls.strlen(errstr)>0 then
					Cls.echo "0,"&errstr&","","
				else
					Cls.echo "11,"&filename&","&getfile_ext(fileext)&","&oldname&""
				end if
			next
		set upload=nothing
	end sub
	
	sub jsondb(byval t0)
		dim upload
		dim inputname:inputname="filedata"'表单文件域name
		set upload=new Cls_upload
			upload.filetype=t0
			upload.savepath=Cls.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=204800
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if Cls.strlen(errstr)>0 then
					Cls.echo ("{'err':'错误','msg':{'url':'','localname':'','id':'1'}}")
				else
					Cls.echo ("{'err':'','msg':{'url':'"&filename&"','localname':'"&oldname&"','id':'1'}}")
				end if
			next
		set upload=nothing
	end sub
	
	on error resume next
	dim act:act=Cls.fget("act",0)
	select case act
		case "editor":editordb
		case "swf":swfdb
		case "json":jsondb("gif|jpg|png|jpeg")
		case "json_down":jsondb(Cls.getsys("uptype_file"))
		case "other":islogin:errdb
	end select
%>